package com.situ.stl.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import org.springframework.beans.factory.annotation.Autowired;
import com.situ.stl.interceptor.AdminInterceptor;

@Configuration
public class WebConfig implements WebMvcConfigurer{
	
	@Value("${file.path}")
	private String path;
	
	
	@Autowired
	private AdminInterceptor adminInterceptor;

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/upload/**").addResourceLocations("file:"+path);
	}
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(adminInterceptor)
			.addPathPatterns("/**")
			.excludePathPatterns(
					"/upload/**",
					"/captcha",
					"/admin/login", "/admin/getLogin",
					"/swagger**/**","/**/api-docs/**");
	}

	/**
	 * 跨域
	 * @param registry
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		// 添加映射路径
		registry.addMapping("/**")
				.allowedOriginPatterns("*") // 允许哪些域的请求，星号代表允许所有
				.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE") // 允许的方法
				.allowedHeaders("*") // 允许的头部设置
				.allowCredentials(true) // 是否发送cookie
				.maxAge(168000); // 预检间隔时间
	}
}
